package com.org.utils.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

import com.org.utils.conf.ConfigureMacro;

public class FileUploadHandler  
{
	private File file;
	private String fileFileName;
	private String fileContentType;
	private String savePath;
	
	public void upload()
    {
    	String roots =ConfigureMacro.getMacro("img_root_directory");
    	String temp_directory =ConfigureMacro.getMacro("img_directory_temp");    	 
    	String fileName = this.getFileFileName();
    	 
    	try{
    		InputStream is = new FileInputStream(file); 
    		final File deskDrir = new File(roots+savePath);
    		if(!deskDrir.exists()){
    			deskDrir.mkdirs();
    		}
    		final File deskFile = new File(roots+savePath,fileName);
    		OutputStream os = new FileOutputStream(deskFile);
    		byte[] bytefer = new byte[1024];
    		int length = 0;
    		while ((length = is.read(bytefer)) != -1) {
    			os.write(bytefer, 0, length);
    		}
    		os.close();
    		is.close();
    		//ServletUtils.setResponse(savePath+"/"+fileName);    		
    		new Thread(new Runnable(){
				public void run() {
					try {
						Thread.sleep(60*60*1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					if(deskFile.exists())deskFile.delete();
				}
    		}).start();
    		
    	}catch(IOException e){
    		e.printStackTrace();
    	}
    	
    }
	public String getUUName(String fileFileName)
	{
		int index=fileFileName.lastIndexOf(".");
		String suffix=fileFileName.substring(index, fileFileName.length());//拿到后缀
		return UUID.randomUUID().toString()+suffix;
	}
	public File getFile()
	{
		return file;
	}

	public void setFile(File file)
	{
		this.file = file;
	}

	public String getFileFileName()
	{
		return getUUName(fileFileName);
	}

	public void setFileFileName(String fileFileName)
	{
		this.fileFileName = fileFileName;
	}

	public String getFileContentType()
	{
		return fileContentType;
	}

	public void setFileContentType(String fileContentType)
	{
		this.fileContentType = fileContentType;
	}

	public String getSavePath()
	{
		return savePath;
	}

	public void setSavePath(String savePath)
	{
		this.savePath = savePath;
	}
	
	
}
